package com.zg.utils;


import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * 单例线程池
 */
public class ThreadPool {
    private static volatile ExecutorService threadPool;

    private ThreadPool(){

    }

    public static ExecutorService getThreadPool(){
        if (threadPool == null){
            synchronized (ThreadPool.class){
                if (threadPool == null){
                    threadPool = new ThreadPoolExecutor(
                            Runtime.getRuntime().availableProcessors(),
                            Runtime.getRuntime().availableProcessors()*2+1,
                            60,
                            TimeUnit.SECONDS,
                            new LinkedBlockingDeque<>(10),
                            Executors.defaultThreadFactory(),
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return threadPool;
    }
}
